package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wx_coupon.WXShowCouponListVO;
import com.cskaoyan.bean.wx_coupon.WXShowMyListCouponVO;
import com.cskaoyan.bean.wx_coupon.WXShowSelectCouponVO;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.wx.coupon.WXCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description:
 * @Author: WD created on 2021-09-19 12:31
 * @Version:
 */


@RestController
@RequestMapping("wx/coupon")
public class WXCouponController {
    @Autowired
    WXCouponService couponService;
    @Autowired
    UserService userService;

    @RequestMapping("mylist")
    public BaseRespVo getMyList(Integer page, Integer limit, Integer status) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        BaseParam baseParam = new BaseParam();
        baseParam.setLimit(limit);
        baseParam.setPage(page);

        User user = userService.getUser(username);
        WXShowMyListCouponVO myCouponList = couponService.getMyCouponList(baseParam, user.getId(), status);

        return BaseRespVo.ok(myCouponList);

    }


    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        String code = (String) map.get("code");
        User user = userService.getUser(username);
        Integer integer = couponService.exchangeByCode(code, user);
        switch (integer) {
            case 200:
                return BaseRespVo.ok();
            case 400:
                return BaseRespVo.fail("未知错误，请联系管理员");
            case 405:
                return BaseRespVo.fail("领取超出最大数量限制");
            case 401:
                return BaseRespVo.fail("来晚了，已被领光");
            case 404:
                return BaseRespVo.fail("code不存在");
            case 402:
                return BaseRespVo.fail("来晚了，已过期");
            default:
                return BaseRespVo.fail("未知错误.请联系管理员!");
        }
    }

    @RequestMapping("selectlist")
    public BaseRespVo selectList(Integer cartId, Integer grouponRulesId) {

        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        User user = userService.getUser(username);
        Integer id = user.getId();

        WXShowSelectCouponVO selectCoupon = couponService.getSelectCoupon(id);


        return BaseRespVo.ok(selectCoupon);
    }

    @RequestMapping("list")
    public BaseRespVo getCouponList(Integer page, Integer limit) {
        BaseParam baseParam = new BaseParam();
        baseParam.setLimit(limit);
        baseParam.setPage(page);
        WXShowCouponListVO couponList = couponService.getCouponList(baseParam);
        return BaseRespVo.ok(couponList);
    }


    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody Map<String,Integer> map){
        Integer cid = map.get("couponId");

        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        User user = userService.getUser(username);
        Integer uid = user.getId();

        Integer receive = couponService.receive(cid, uid);
        switch (receive) {
            case 200:
                return BaseRespVo.ok();
            case 402:
                return BaseRespVo.fail("未知错误，请联系管理员");
            case 405:
                return BaseRespVo.fail("领取超出最大数量限制");
            case 401:
                return BaseRespVo.fail("来晚了，已被领光");
            case 400:
                return BaseRespVo.fail("来晚了，已过期");
            default:
                return BaseRespVo.fail("未知错误.请联系管理员!");
        }



    }


}
